﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple_PM.Models;

namespace Simple_PM.Services.Task
{
    public class TaskService : ITaskService
    {
        // 1 - New Task
        // 2 - Change Request
        // 3 - Bugs
        // 4 - To Do
        // 5 - Misc

        public List<SelectListItem> GetCategoriesList()
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "New Task", Value = "1" });
            list.Add(new SelectListItem { Text = "Change Request", Value = "2" });
            list.Add(new SelectListItem { Text = "Bugs", Value = "3" });
            list.Add(new SelectListItem { Text = "To Do", Value = "4" });
            list.Add(new SelectListItem { Text = "Miscellaneous", Value = "5" });

            return list;
        }

        public CategoriesCount GetCategoriesCount(string username)
        {
            var counts = new CategoriesCount();

            using(var context = new SimplePmEntities())
            {
                long userId = context.Users.Where(m => m.UserName == username).FirstOrDefault().UserId;
                var tasks = context.DashboardViews.Where(m => m.UserId == userId && m.TaskStatus != 1).ToList();

                counts.NewFeatureCount = tasks.Where(m => m.CategoryId == 1).Count();
                counts.ChangeRequestCount = tasks.Where(m => m.CategoryId == 2).Count();
                counts.BugsCount = tasks.Where(m => m.CategoryId == 3).Count();
                counts.TodoCount = tasks.Where(m => m.CategoryId == 4).Count();
                counts.MiscCount = tasks.Where(m => m.CategoryId == 5).Count();
            }

            return counts;
        }

        public List<TaskModel> LoadTasks(string filterStatus, string category, string username)
        {
            var taskList = new List<TaskModel>();

            using(var context = new SimplePmEntities())
            {
                long userId = context.Users.Where(m => m.UserName == username).FirstOrDefault().UserId;
                var tasks = context.DashboardViews.Where(m => m.UserId == userId).ToList();

                // New Feature / Change Request / Bugs / Todos / Miscellaneous 
                if(category == "NF")
                {
                    tasks = tasks.Where(m => m.CategoryId == 1).ToList();
                }
                else if (category == "CR")
                {
                    tasks = tasks.Where(m => m.CategoryId == 2).ToList();
                }
                else if (category == "BUG")
                {
                    tasks = tasks.Where(m => m.CategoryId == 3).ToList();
                }
                else if (category == "TODO")
                {
                    tasks = tasks.Where(m => m.CategoryId == 4).ToList();
                }
                else if (category == "MISC")
                {
                    tasks = tasks.Where(m => m.CategoryId == 5).ToList();
                }
                
                // All / Completed
                // 1 = completed
                if(filterStatus == "Completed")
                {
                    tasks = tasks.Where(m => m.TaskStatus == 1).ToList();
                    tasks = tasks.OrderByDescending(m => m.DateCompleted).ToList();
                }
                else if(filterStatus == "Mix")
                {
                    tasks = tasks;
                }
                else //if(filterStatus == "All")
                {
                    tasks = tasks.Where(m => m.TaskStatus != 1).ToList();
                }


                foreach (var item in tasks)
                {
                    if (item.DateCompleted != null)
                    {
                        taskList.Add(new TaskModel
                                         {
                                             TaskId = item.TaskId,
                                             TaskTitle = item.TaskTitle,
                                             CategoryId = item.CategoryId,
                                             TaskStatus = item.TaskStatus,
                                             TaskScheduled = item.DateScheduled,
                                             TaskCompleted = item.DateCompleted.Value.ToShortDateString() + " " + item.DateCompleted.Value.ToShortTimeString()
                                         });
                    }
                    else
                    {
                        taskList.Add(new TaskModel
                        {
                            TaskId = item.TaskId,
                            TaskTitle = item.TaskTitle,
                            CategoryId = item.CategoryId,
                            TaskStatus = item.TaskStatus,
                            TaskScheduled = item.DateScheduled,
                            TaskCompleted = item.DateCompleted.ToString()
                        });
                    }
                }
            }

            taskList = taskList.OrderBy(m => m.TaskScheduled ?? DateTime.MaxValue).ToList();
            
            return taskList;
        }

        public bool AddTask(TaskModel model, string username)
        {
            bool isTaskAdded = false;

            using(var context = new SimplePmEntities())
            {
                long userId = context.Users.Where(m => m.UserName == username).FirstOrDefault().UserId;
                context.TaskLists.AddObject(new TaskList{CategoryId = model.CategoryId, TaskTitle = model.TaskTitle, UserId = userId, DateScheduled=model.TaskScheduled});
                context.SaveChanges();
            }

            return isTaskAdded;
        }

        public TaskModel GetSingleTask(int intTaskId)
        {
            TaskModel model = new TaskModel();

            using(var context = new SimplePmEntities())
            {
                var task = context.TaskLists.Where(m => m.TaskId == intTaskId).FirstOrDefault();
                model.TaskTitle = task.TaskTitle;
                model.TaskId = task.TaskId;
                model.CategoryId = task.CategoryId;
            }

            return model;
        }

        public bool UpdateSingleTask(TaskModel model)
        {
            bool isUpdated = false;

            using(var context = new SimplePmEntities())
            {
                var task = context.TaskLists.Where(m => m.TaskId == model.TaskId).FirstOrDefault();
                task.TaskTitle = model.TaskTitle;
                task.CategoryId = model.CategoryId;

                context.SaveChanges();
            }

            return isUpdated;
        }

        public bool CloseTask(int taskId)
        {
            bool isClosed = false;

            using(var context = new SimplePmEntities())
            {
                var task = context.TaskLists.Where(m => m.TaskId == taskId).FirstOrDefault();
                task.TaskStatus = 1;
                task.DateCompleted = DateTime.Now;
                context.SaveChanges();
            }

            return isClosed;
        }

        public bool ReopenTask(int taskId)
        {
            bool isReopened = false;

            using(var context = new SimplePmEntities())
            {
                var task = context.TaskLists.Where(m => m.TaskId == taskId).FirstOrDefault();
                task.TaskStatus = 0;
                task.DateCompleted = null;

                context.SaveChanges();
            }

            return isReopened;
        }

        public bool DeleteTask(int taskId)
        {
            bool isDeleted = false;

            using (var context = new SimplePmEntities())
            {
                var task = context.TaskLists.Where(m => m.TaskId == taskId).FirstOrDefault();
                context.TaskLists.DeleteObject(task);

                context.SaveChanges();
                isDeleted = true;
            }

            return isDeleted;
        }
    }

    public interface ITaskService
    {
        List<SelectListItem> GetCategoriesList();
        CategoriesCount GetCategoriesCount(string username);
        List<TaskModel> LoadTasks(string filterStatus, string category, string username);
        bool AddTask(TaskModel model, string username);
        TaskModel GetSingleTask(int intTaskId);
        bool UpdateSingleTask(TaskModel model);
        bool CloseTask(int taskId);
        bool ReopenTask(int taskId);
        bool DeleteTask(int taskId);
    }
}